﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aspfm.DataBase;
using Aspfm.Dal;
using Aspfm.Main;

namespace Aspfm.DataBase
{
    public partial class UcParams : UserControl
    {
        public UcParams()
        {
            InitializeComponent();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            textBox1.Tag = null;
            textBox1.Text = "";
            dataGridView1.Rows.Clear();
            if (treeView1.SelectedNode != null && treeView1.SelectedNode != treeView1.Nodes[0])
            {
                treeView1.SelectedNode = treeView1.Nodes[0];
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node != null && node.Level == 1)
            {
                if (node.Index == 0) return;
                 DialogResult dia = MessageBox.Show("是否确定删除？", "删除提示", MessageBoxButtons.YesNo);
                 if (dia == DialogResult.Yes)
                 {
                     if (node.Tag is DataParams)
                     {
                         DataParams dp = (DataParams)node.Tag;
                         Form1.datalist.DataParamsList.Remove(dp);
                         ConfigTool.SaveConn(Form1.datalist);

                         node.Remove();
                     }
                 }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();

            string typename = textBox1.Text.Trim().ToUpper();
            if (string.IsNullOrEmpty(typename))
            {
                MessageBox.Show("类型名称为空");
                return;
            }
            if (typename.Contains(","))
            {
                MessageBox.Show("类型名称不允许包含“,”号");
                return;
            }
            DataParams dp = null;
            TreeNode node = treeView1.SelectedNode;
            if (typename!=node.Text.ToUpper() && checkHadType(typename))
            {
                MessageBox.Show("类型已经存在，不允许添加：" + typename);
                return;
            }
            if (textBox1.Tag == null)
            {
                dp = new DataParams();
                Form1.datalist.DataParamsList.Add(dp);
                node = new TreeNode(typename);
                node.Tag = dp;
                treeView1.Nodes[0].Nodes.Add(node);
            }
            else if (textBox1.Tag is DataParams)
            {
                dp = (DataParams)textBox1.Tag;
                node.Text = typename;
            }
            List<DataParamsInfo> paramslist = new List<DataParamsInfo>();
            string hadstr = ",";
            dp.IsSecret = checkBox1.Checked;
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                DataParamsInfo dpinfo = new DataParamsInfo();
                dpinfo.Key = GetCell(dr, "ParamsKey").ToUpper();
                dpinfo.KeyValue = GetCell(dr, "ParamsValue");
                dpinfo.KeyRemark = GetCell(dr, "Remark");
                if (!string.IsNullOrEmpty(dpinfo.Key))
                {
                    if (!hadstr.Contains("," + dpinfo.Key + ","))
                    {
                        hadstr += dpinfo.Key + ",";
                        paramslist.Add(dpinfo);
                    }
                    else
                    {
                        MessageBox.Show("参数名已经存在，不允许重复：" + dpinfo.Key);
                        return;
                    }
                }
            }
            dp.Key = typename;
            dp.paramslist = paramslist;
            treeView1.SelectedNode = node;
            ConfigTool.SaveConn(Form1.datalist);
        }

        private string GetCell(DataGridViewRow dr,string colname)
        {
            if (dr.DataGridView.Columns.Contains(colname))
            {
                object obj = dr.Cells[colname].Value;
                if (obj != null)
                    return obj.ToString().Trim();
            }
            return "";
        }
        public bool checkHadType(string typename)
        {
            typename = typename.Trim().ToUpper();
            if (Form1.datalist != null && Form1.datalist.DataParamsList != null)
            {
                foreach (DataParams dp in Form1.datalist.DataParamsList)
                {
                    if (dp.Key == typename)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        public void Init()
        {
            if (Form1.datalist != null && Form1.datalist.DataParamsList != null)
            {
                foreach (DataParams dp in Form1.datalist.DataParamsList)
                {
                    if (dp.Key == treeView1.Nodes[0].Nodes[0].Text.ToUpper())
                    {
                        treeView1.Nodes[0].Nodes[0].Tag = dp;
                    }
                    else
                    {
                        TreeNode node = new TreeNode(dp.Key);
                        node.Tag = dp;
                        treeView1.Nodes[0].Nodes.Add(node);
                    }
                }
            }
            if (Form1.datalist.DataParamsList == null)
                Form1.datalist.DataParamsList = new List<DataParams>();
            treeView1.Nodes[0].ExpandAll();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            checkBox1.Checked = false;
            TreeNode node = null;
            if (e != null && e.Node != null)
            {
                node = e.Node;
            }
            textBox1.Tag = null;
            textBox1.Text = "";
            dataGridView1.Rows.Clear();
            if (node != null)
            {
                if (node.Index == 0 && node.Level==1)
                {
                    textBox1.Enabled=false;
                    if (node.Tag == null)
                    {
                        DataParams dpwebinfo = new DataParams();
                        dpwebinfo.Key = node.Text;
                        Form1.datalist.DataParamsList.Insert(0, dpwebinfo);
                        node.Tag = dpwebinfo;
                    }
                }
                else
                {
                    textBox1.Enabled=true;
                }
                if (node.Tag is DataParams)
                {
                    DataParams dp=(DataParams)node.Tag;
                    textBox1.Text = dp.Key;
                    checkBox1.Checked = dp.IsSecret;
                    textBox1.Tag = dp;
                    if (dp.paramslist != null)
                    {
                        foreach (DataParamsInfo dpi in dp.paramslist)
                        {
                            dataGridView1.Rows.Add(dpi.Key, dpi.KeyValue, dpi.KeyRemark);
                        }
                    }
                }
            }
        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            TreeNode node = treeView1.GetNodeAt(e.X, e.Y);
            if (node != null)
            {
                treeView1.SelectedNode = node;
            }
            else
            {
                treeView1.SelectedNode = treeView1.Nodes[0];
            }
        }
    }
}
